<?
if (!defined('MODULE_FILE')){
die ("cant access file directly!");
}

$mid 	= ( ( true == isset( $_GET['mid'] ) && true == valStr( $_GET['mid'] ) ) ? $_GET['mid'] : NULL );
$smid 	= ( ( true == isset( $_GET['smid'] ) && true == valStr( $_GET['smid'] ) ) ? $_GET['smid'] : NULL );

autoLoader();

$objBasePatient 	= new BasePatients();
$objPatients 		= new Patients();
$objPatientTypes 	= new PatientCatagories();

$straction = 'insert';
if( true == isset( $_GET['patient_id'] ) && true == valStr( $_GET['patient_id'] ) ) {	
$straction = 'update';				
}
?>
<html>
<head>
<?php require_once 'commonJS.php'; ?>

<style type="text/css">
	td, th {
		width: 18%;
	}
	td:nth-child(2) {
		width: 120px;
	}
	td:nth-child(3) {
		margin-left: 10px;
	}
</style>

<script>
$(document).ready(function(){
	$( "#from, #birth_date" ).datepicker({
		dateFormat: 'dd-mm-yy',
		showOn: "button",
		buttonImage: "images/calendar.gif",
		buttonImageOnly: true,
		changeMonth: true,
		changeYear: true,
		yearRange: "-90"
	});
});

	function calculateAge() {

		var birthday 	= $('#birth_date' ).val();
		birthday 		= birthday.replace(/-/g, "/");

		var today = new Date();
		var birthDate = new Date(birthday);
		var age = today.getFullYear() - birthDate.getFullYear();
		var m = today.getMonth() - birthDate.getMonth();
		if (m < 0 || (m === 0 && today.getDate() < birthDate.getDate())) {
			age--;
		}

		if( age > 0 ) {
			$('#age').val( age );
		}
	}

	function printDoc() {
		document.print_case_paper.submit();
	}
</script>
<style type="text/css">
input[type=text], textarea, select {
	-webkit-transition: all 0.30s ease-in-out;
	-moz-transition: all 0.30s ease-in-out;
	-ms-transition: all 0.30s ease-in-out;
	-o-transition: all 0.30s ease-in-out;
	outline: none;
	padding: 3px 0px 3px 3px;
	margin: 5px 1px 3px 0px;
	border: 1px solid #DDDDDD;
}
 
input[type=text]:focus, textarea:focus, select:focus, option:focus {
	box-shadow: 0 0 5px rgba(81, 203, 238, 1);
	padding: 3px 0px 3px 3px;
	margin: 5px 1px 3px 0px;
	border: 1px solid rgba(81, 203, 238, 1);
}
</style>
</head>
<body>
<br>
<div class="col-lg-12">
	<div>
		<!-- <div style="float: left; width: 200px;"><a href="modules.php?file=case_papers&mid=<? echo $mid; ?>&smid=<? echo $smid; ?>" title="New Case Paper"><img src="images/icons/add_new.png" width="40" height="40">New case paper</a></div> -->
	</div>
	<div class="panel panel-primary">
		<div style="float: left;" class="panel-heading"><i class="fa fa-bell fa-fw"></i>Casepaper</div>
		<div style="width: 40%; margin-left: 20%;"><a href="modules.php?file=old_case_papers&mid=<? echo $mid; ?>&smid=<? echo $smid; ?>" title="Old Case Paper"><img src="images/icons/update.png" width="40" height="40">Old case paper</a></div>
		
		<div class="panel-body">
			<?php 
				$strAction = 'insert';
				
				if( true == isset( $_GET['patient_id'] ) && true == valStr( $_GET['patient_id'] ) ) {
					$strAction = 'update';
				}
			?>
		<form METHOD="POST" action="modules.php?file=insert_patient&action=<? echo $strAction; ?>&type=insert&mid=<? echo $mid; ?>&smid=<? echo $smid; ?>" id="formSubmit" name="formSubmit" enctype="multipart/form-data">
			
			<?php
				$intPatientId 		= NULL;
				$strFirstName 		= NULL;
				$strMiddleName 		= NULL;
				$strLastName 		= NULL;
				$strMotherName 		= NULL;
				$intAge 			= NULL;
				$strGender 			= NULL;
				$strBirthDate 		= NULL;
				$strMaritalStatus 	= NULL;
				$strOccupaction 	= NULL;
				$strAddress 		= NULL;
				$strHomeNumber 		= NULL;
				$strOfficeNumber 	= NULL;
				$strMobileNumber 	= NULL;
				$strEmailAddress 	= NULL;
				$intPatientTypeId 	= NULL;
				$strPatientAreaId	= NULL;
				
				$intReferancePatientId 		= NULL;
				
				if( true == isset( $_GET['patient_id'] ) && true == valStr( $_GET['patient_id'] ) ) {
					$intPatientId = $_GET['patient_id'];
					
					$objBasePatient = $objPatients->fetchPatientsById( $intPatientId );
					
					if( true == valObj( $objBasePatient, 'BasePatients' ) ) {
						$intPatientId 		= $objBasePatient->getId();
						$strFirstName 		= $objBasePatient->getFirstName();
						$strMiddleName 		= $objBasePatient->getMiddleName();
						$strLastName 		= $objBasePatient->getLastName();
						$strMotherName 		= $objBasePatient->getMotherName();
						$intAge 			= $objBasePatient->getAge();
						$strGender 			= $objBasePatient->getGender();
						$strBirthDate 		= date( 'd/m/Y', strtotime( $objBasePatient->getBirthDate() ) );
						$strMaritalStatus 	= $objBasePatient->getMaritalStatus();
						$strOccupaction 	= $objBasePatient->getOccupaction();
						$strAddress 		= $objBasePatient->getAddress();
						$strHomeNumber 		= $objBasePatient->getHomeNumber();
						$strOfficeNumber 	= $objBasePatient->getOfficeNumber();
						$strMobileNumber 	= $objBasePatient->getMobileNumber();
						$strEmailAddress 	= $objBasePatient->getEmailAddress();
						$intPatientTypeId 	= $objBasePatient->getPatientTypeId();
						$strPatientAreaId 	= $objBasePatient->getPatientAreaId();
						
						$intReferancePatientId = $objBasePatient->getPatientId();
					}
				} else if( true == valObj( $objBasePatient, 'BasePatients' ) ) {
					$intPatientId = $objBasePatient->fetchNextId();
				}
			?>
				<table width="100%" cellspacing="0" cellpadding="3" border="0">
		
					<tr><td height="10" colspan="4"></td></tr>
					<tr valign="top" style="background: #eee;">
						<td>Patient ID</td>
						<td><input id="patient_id" Name="patient_id" type="text" class="" style="width:100px;" readonly="readonly" value="<?php echo $intPatientId; ?>"></td>
						<td class="mandatory" >Date Time</td>
						<td><input class="validate[required]" readonly type="text" name="from" id="from" value="<?php echo date( 'd/m/Y' ); ?>" style="width:202px;">
					</tr>
					
					<tr><td colspan="4" height="10px">&nbsp;</td></tr>
					<tr valign="top" style="background: #eee;">
						<td >First Name</td>
						<td><input id="first_name" Name="first_name" type="text" class="validate[required]" style="width:202px;" value="<?php echo $strFirstName; ?>"></td>
						<td >Middle Name</td>
						<td><input id="middle_name" Name="middle_name" type="text" class="" style="width:202px;" value="<?php echo $strMiddleName; ?>"></td>
					</tr>
					
					<tr><td colspan="4" height="10px">&nbsp;</td></tr>
					<tr valign="top" style="background: #eee;">
						<td >Last Name</td>
						<td><input id="last_name" Name="last_name" type="text" class="validate[required]" style="width:202px;" value="<?php echo $strLastName; ?>"></td>
						<td >Upload Photo</td>
						<td><input type="file" name="photo" id="photo" accept="image/gif, image/jpeg, image/png, image/jpg"></td>
					</tr>
					
					<tr><td colspan="4" height="10px">&nbsp;</td></tr>
					<tr valign="top" style="background: #eee;">
						<td width="140">Birth Date</td>
						<td>
							<input type="text" class="" name="birth_date" id="birth_date" style="width:150px;" value="<?php echo $strBirthDate; ?>" />
						</td>
						<td >Age</td>
						<td><input onfocus="calculateAge()" placeholder="Click here to calculate age." id="age" Name="age" type="text" class="" style="width:202px;" value="<?php echo $intAge; ?>"></td>
					</tr>
					
					<tr><td colspan="4" height="10px">&nbsp;</td></tr>
					<tr style="background: #eee;">
						<td >Gender</td>
						<td>
							<select onfocus="calculateAge()" class="validate[required]" name="gender" id="gender" style="width:202px;">
								<option value=''>-Select-</option>
								<option value='1' <?php echo ( ( 1 == $strGender ) ? 'selected' : '' ); ?>>Male</option>
								<option value='2' <?php echo ( ( 2 == $strGender ) ? 'selected' : '' ); ?>>Female</option>
								<option value='3' <?php echo ( ( 3 == $strGender ) ? 'selected' : '' ); ?>>Other</option>
							</select>
						</td>
						
						<td >Occupation</td>
						<td><input id="occupaction" Name="occupaction" type="text" class="" style="width:202px;" value="<?php echo $strOccupaction; ?>"></td>
					</tr>
					
					<tr><td colspan="4" height="10px">&nbsp;</td></tr>
					<tr valign="top" style="background: #eee;">
						<td >Mobile No.</td>
						<td><input id="mobile_number" Name="mobile_number" type="text" class="validate[required]" style="width:202px;" value="<?php echo $strMobileNumber; ?>"></td>
						<td>&nbsp;</td>
						<td>&nbsp;</td>
					</tr>
					
					<tr><td colspan="4" height="10px">&nbsp;</td></tr>
					<tr valign="top" style="background: #eee;">
						<td >Marital Status</td>
						<td><input id="marital_status" Name="marital_status" type="text" class="validate[required]" style="width:202px;" value="<?php echo $strMaritalStatus; ?>" ></td>
						
						<td >Address</td>
						<td rowspan="4"><textarea name="address" rows="4" cols="30" style="resize:none;"><?php echo $strAddress; ?></textarea></td>
					</tr>
					
					<tr><td colspan="4" height="10px">&nbsp;</td></tr>
					<tr valign="top" style="background: #eee;">
						<td >Patient Type</td>
						<td>
							<select class="validate[required]" name="patient_type_id" id="patient_type_id" style="width:202px;">
								<option value=''> ------ Select ------ </option>
							<?php
								$arrobjPatientTypes = $objPatientTypes->fetchAllPatientCatagories();
								
								if( true == valStr( $arrobjPatientTypes ) ) {
									foreach( $arrobjPatientTypes as $objPatientType ) {
										echo '<option' . ( ( $intPatientTypeId == $objPatientType->getId() ) ? ' selected' : '' ) . ' value=' . $objPatientType->getId() . '>' . $objPatientType->getName() . '</option>';
									}
								}
							?>
							</select>
						</td>
					</tr>
					
					<tr><td colspan="4" height="10px">&nbsp;</td></tr>
					<tr style="background: #eee;">
						<td >Patient Area</td>
						<td>
							<select class="validate[required]" name="patient_area_id" id="patient_area_id" style="width:202px;">
								<option value=''> ------ Select ------ </option>
								<option value='1' <?php echo ( 1 == $strPatientAreaId ) ? 'selected' : NULL; ?>>Pune Cantonment Board Area</option>
								<option value='2' <?php echo ( 2 == $strPatientAreaId ) ? 'selected' : NULL; ?>>Out Of Pune Cantonment Board Area</option>
							
							</select>
						</td>
						<td >Upload ID Proof</td>
						<td><input type="file" name="idproof" id="idproof"></td>
					</tr>
					
					<tr><td colspan="4" height="10px">&nbsp;</td></tr>
					<tr valign="top" style="background: #eee;">
						<td class="mandatory" width="220">Fee (Amount)</td>
						<td ><input id="amount_paid" Name="amount_paid" type="text" class="validate[required]" style="width:202px;" value="<?php echo $intFeeAmount; ?>"></td>
						<td >Upload Resident Proof</td>
						<td><input type="file" name="resproof" id="resproof"></td>
					</tr>
					
					<tr><td colspan="4" height="20px">&nbsp;</td></tr>
					<tr valign="top" >
						<td class="mandatory" width="220">&nbsp;</td>
						<td colspan="3">
							<?php
								if( true == isset( $_GET['patient_id'] ) && true == valStr( $_GET['patient_id'] ) ) {
									echo "<input type=hidden name=operation value=update>";
									echo "<input type=submit value=UPDATE id=Submit name=Submit class='btn btn-primary'>&nbsp;&nbsp;&nbsp;&nbsp;";
									echo "<a href=modules.php?file=print_case_paper&type=print&patient_id=" . ( ( true == isset( $_GET['patient_id'] ) && true == valStr( $_GET['patient_id'] ) ) ? $_GET['patient_id'] : NULL ) . "&case_paper_id=" . ( ( true == isset( $_GET['case_paper_id'] ) && true == valStr( $_GET['case_paper_id'] ) ) ? $_GET['case_paper_id'] : NULL ) . "&mid=" . $mid . "&smid=" . $smid . "><input type=button name=print value=Print class='btn btn-primary'/></a> &nbsp;&nbsp;&nbsp;";
								} else {
									echo "<input type=hidden name=operation value=insert>";
									echo "<input type=submit value=SUBMIT id=Submit name=Submit class='btn btn-primary'>";
								}
							?>
							<a href="modules.php?file=view_patients&type=view&mid=<? echo $mid; ?>&smid=<? echo $smid; ?>"><input class='btn btn-primary' type="button" name="calcle" value="Cancel" /></a>
						</td>
					</tr>
				</table>
			</form>
		</div>
	</div>
</div>
<form action="modules.php?file=print_case_paper&type=print&patient_id=<?php echo ( true == isset( $_GET['patient_id'] ) && true == valStr( $_GET['patient_id'] ) ) ? $_GET['patient_id'] : NULL; echo '&mid=' . $mid . '&smid=' . $smid; ?>" method="post" name="print_case_paper" >
</form>
</body>
</html>